package br.com.inner.modelo;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import br.com.inner.custom.CustomError;
import br.com.inner.custom.CustomException;
import javax.persistence.ManyToMany;
import javax.persistence.CascadeType;

import java.text.ParseException;

@Entity
@Table(name="Job")
/*@NamedQueries({
	@NamedQuery(name="listarJob",
			query="Select j from Job j where j.destinatarios = :cdDestinatario")
})*/
public class Job {
	
	@Id
	@GeneratedValue(generator="JobTab")
	@TableGenerator(name="JobTab", table="TABLE_GEN",
            pkColumnName="ID_NAME", valueColumnName="ID_VAL",
            pkColumnValue="JOB_GEN", allocationSize=1)
	@Column(name="cdJob", length=5)
	private BigDecimal cdJob;
	
	@Column(name="titulo", length=50)
	private String titulo;
	
	@Column(name="cliente", length=50)
	private String cliente;
	
	@Column(name="CPB", length=50)
	private String CPB;
	
	@Column(name="tempo", length=50)
	private BigDecimal tempo;
	
	@Column(name="nmArquivo", length=50)
	private String nmArquivo;
	
	@Column(name="tamArquivo", length=50)
	private BigDecimal tamArquivo;
	
	@Column(name="nmMiniArquivo", length=50)
	private String nmMiniArquivo;
	
	@Column(name="tamMiniArquivo", length=50)
	private BigDecimal tamMiniArquivo;
	
	@Column(name="dtCadastro")
	@Temporal(TemporalType.TIMESTAMP)
	private Date dtCadastro;
	
	@ManyToOne
	@JoinColumn(name="cdUsuario", referencedColumnName = "cdUsuario")
	private Usuario usuario;
	
	/*@ManyToMany
	@JoinTable(joinColumns=@JoinColumn(name="cdJob", referencedColumnName = "cdJob"), inverseJoinColumns = @JoinColumn(name = "cdEmpresa", referencedColumnName = "cdEmpresa"))
	private List<Empresa> destinatarios;*/
	
	@OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE }, mappedBy="job")
	private List<JobEmpresa> destinatarios1;
	
	@Transient
	private String[] destinatariosAux;

	public BigDecimal getCdJob() {
		return cdJob;
	}

	public void setCdJob(BigDecimal cdJob) {
		this.cdJob = cdJob;
	}

	/*public List<Empresa> getDestinatarios() {
		return destinatarios;
	}

	public void setDestinatarios(List<Empresa> destinatarios) {
		this.destinatarios = destinatarios;
	}*/

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public String getCliente() {
		return cliente;
	}

	public void setCliente(String cliente) {
		this.cliente = cliente;
	}

	public String getCPB() {
		return CPB;
	}

	public void setCPB(String cpb) {
		CPB = cpb;
	}

	public String getNmArquivo() {
		return nmArquivo;
	}

	public void setNmArquivo(String nmArquivo) {
		this.nmArquivo = nmArquivo;
	}
	
	public BigDecimal getTamArquivo() {
		return tamArquivo;
	}

	public void setTamArquivo(BigDecimal tamArquivo) {
		this.tamArquivo = tamArquivo;
	}

	public String getNmMiniArquivo() {
		return nmMiniArquivo;
	}

	public void setNmMiniArquivo(String nmMiniArquivo) {
		this.nmMiniArquivo = nmMiniArquivo;
	}
	
	public BigDecimal getTamMiniArquivo() {
		return tamMiniArquivo;
	}

	public void setTamMiniArquivo(BigDecimal tamMiniArquivo) {
		this.tamMiniArquivo = tamMiniArquivo;
	}

	public BigDecimal getTempo() {
		return tempo;
	}

	public void setTempo(BigDecimal tempo) {
		this.tempo = tempo;
	}

	public String getTitulo() {
		return titulo;
	}

	public void setTitulo(String titulo) {
		this.titulo = titulo;
	}
	
	@Transient
	private String dtCadastroFmt;
	
	public String getDtCadastroFmt() {
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		Date date = this.dtCadastro;
		this.dtCadastroFmt = dateFormat.format(date);
			
		return dtCadastroFmt;
	}

	
	public Date getDtCadastro() {
		return dtCadastro;
	}

	public void setDtCadastro(Date dtCadastro) {
		this.dtCadastro = dtCadastro;
	}
	
	public void setDtCadastro(String dtCadastro)throws CustomException {
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
		try {
			this.dtCadastro = dateFormat.parse(dtCadastro);
		} catch (ParseException e) {
			throw new CustomException(new CustomError(e,"erro.converter.string.data"));
		}
	}


	public String[] getDestinatariosAux() {
		return destinatariosAux;
	}

	public void setDestinatariosAux(String[] destinatariosAux) {
		this.destinatariosAux = destinatariosAux;
	}
	
	public List<JobEmpresa> getDestinatarios1() {
		return destinatarios1;
	}

	public void setDestinatarios1(List<JobEmpresa> destinatarios1) {
		this.destinatarios1 = destinatarios1;
	}
	
	public List<Empresa> getEmpresas() {
		
		ArrayList<Empresa> procList = new ArrayList<Empresa>();
		
		if ( destinatarios1 == null ){
			return procList;
		}
		
		for (JobEmpresa ap : destinatarios1) {
			procList.add(ap.getEmpresa());
		}
		
		return procList;

	}
	
	public void addEmpresa(Empresa emp, Date dtRecebimento, StatusJob status){
		
		if ( destinatarios1 == null ){
			destinatarios1 = new ArrayList<JobEmpresa>();
		}
		destinatarios1.add(new JobEmpresa(this, emp , dtRecebimento, status));
	}
}
